package inputstream01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.stream.Stream;

/*
FileInputStream成员方法
       int read(byte[] b)
          1.从文件中读取尽可能多的字节，去装到字节数组中去，0索引存放文件第一个字节，以此类推
          2.返回值代表每次从文件中读取的字符个数

 String类的构造方法
    String(byte[] bytes)
          通过使用平台的默认字符集解码指定的 byte 数组，构造一个新的 String。
 */
public class FileInputStream02 {
    public static void main(String[] args) throws IOException {
        //method01();
        FileInputStream fis = new FileInputStream("Day23/6.txt");

        byte[] bytes = new byte[2];

        /*int length = fis.read(bytes);
        System.out.println(length + " " + new String(bytes));// 2  ab

        int length2 = fis.read(bytes);
        System.out.println(length2 + " " + new String(bytes));// 2  cd

        int length3 = fis.read(bytes);
        System.out.println(length3 + " " + new String(bytes));// 1 ed

        int length4 = fis.read(bytes);
        System.out.println(length4 + " " + new String(bytes));// -1 ed

        int length5 = fis.read(bytes);
        System.out.println(length5 + " " + new String(bytes));//-1 ed*/



        int length;
        while ((length=fis.read(bytes))!=-1) {

            System.out.println(new String(bytes, 0, length)) ;
        }


        fis.close();

    }

    private
    static void method01() throws IOException {
        FileInputStream fis = new FileInputStream("Day23/6.txt");

        byte[] bytes = new byte[2];
        fis.read(bytes);
        /*for (byte b : bytes) {
            System.out.println((char) b);
        }*/
        System.out.println(new String(bytes));//“ab”

        fis.close();
    }
}
